﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Drawing;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;


/*
Developed By: Bhupender Bindra
Modified By: Sujeet Kumar
Developed On: 11th Nov 2010
Modified on 6 Sep 2011
Description: The code provides the user to project list.   
*/

namespace SiteProjects.My_Projects
{
    public partial class My_ProjectsUserControl : UserControl
    {
        string userId = string.Empty;
        DataTable dTProjectDetail;
        SPUser user;
        protected void Page_Load(object sender, EventArgs e)
        {
            user = SPContext.Current.Web.CurrentUser;

            try
            {
                
                GetProjectDetail();
               
            }
            catch (Exception ex)
            {
                lblMessage.Visible = true;
                lblMessage.Text = ex.Message;
                lblMessage.ForeColor = Color.Red;
            }
        }


        /// <summary>
        /// Paging
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvProjectList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvProjectList.PageIndex = e.NewPageIndex;
            gvProjectList.DataBind();
        }

        /// <summary>
        /// This data bound used to get the text and url
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        protected void gvProjectList_ItemDataBound(object sender, GridViewRowEventArgs e)
        {
            LinkButton projectTitle;
            HyperLink hlkTitle;
            Label lblUrl;
            Label lblTitle, lblDescription;
            //website used to get the title,url and display in label and hyperlink

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                lblTitle = (System.Web.UI.WebControls.Label)(e.Row.Cells[0].FindControl("lblTitle"));
                lblUrl = (System.Web.UI.WebControls.Label)(e.Row.Cells[0].FindControl("lblUrl"));
                hlkTitle = (System.Web.UI.WebControls.HyperLink)(e.Row.Cells[1].FindControl("hlkTitle"));
               // projectTitle = (System.Web.UI.WebControls.LinkButton)(e.Row.Cells[1].FindControl("lbtProjectTitle"));
                lblDescription = (System.Web.UI.WebControls.Label)(e.Row.Cells[4].FindControl("lblDescription"));
                hlkTitle.Text = lblTitle.Text;
               // projectTitle.Text=lblTitle.Text;
                hlkTitle.NavigateUrl = lblUrl.Text;
                hlkTitle.Target = "_blank";
                
                //check the description length
                if (lblDescription.Text.Length > 15)
                {
                    lblDescription.Text = lblDescription.Text.Substring(0, 15) + "....";
                }
            }
        }

        /// <summary>
        /// This method is used to get all the project ands its detail for a user.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GetProjectDetail()
        {

            dTProjectDetail = new DataTable();
            dTProjectDetail.Columns.Add("ProjectName", typeof(String));
            dTProjectDetail.Columns.Add("ProjectAuthor", typeof(String));
            dTProjectDetail.Columns.Add("ProjectUrl", typeof(String));
            dTProjectDetail.Columns.Add("CreationDate", typeof(String));
            dTProjectDetail.Columns.Add("ProjectDescription", typeof(String));
            SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
            SPSiteCollection siteCollections = webApplication.Sites;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                foreach (SPSite siteCollection in siteCollections)
                {
                    foreach (SPWeb web in siteCollection.AllWebs)
                    {
                        if (web.DoesUserHavePermissions(user.LoginName, SPBasePermissions.ViewPages) && siteCollection.Url.IndexOf("/projects/") > 1)
                        {
                            DataRow row = dTProjectDetail.NewRow();
                            row["ProjectName"] = web.Title;
                            row["ProjectAuthor"] = web.Author.Name;
                            row["ProjectUrl"] = web.ServerRelativeUrl;
                            row["CreationDate"] = web.Created;
                            row["ProjectDescription"] = web.Description;
                            dTProjectDetail.Rows.Add(row);
                        }
                        web.Dispose();
                    }
                }
            });
            if (dTProjectDetail.Rows.Count != 0)
            {
                gvProjectList.DataSource = dTProjectDetail;
                gvProjectList.DataBind();
            }
            else
            {
                lblMessage.Text = "There are no items to show in this view.";
                lblMessage.Visible = true;
            }
            
        }       


    }
}
